package pooller.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import pooller.LoggCallback;
import pooller.dto.pool.PoolMetaInfoDto;
import pooller.dto.pool.PoolQuestionDto;
import pooller.repository.PoolingRepositoryService;
import pooller.repository.PoolingRepositoryServiceAsync;

import java.util.List;

public class QuestionsPanel extends Composite {
    private PoolMetaInfoDto metaInfoDto;

    interface QuestionsPanelUiBinder extends UiBinder<DockLayoutPanel, QuestionsPanel> {
    }

    private static QuestionsPanelUiBinder ourUiBinder = GWT.create(QuestionsPanelUiBinder.class);

    public static final PoolingRepositoryServiceAsync poolingRepository = GWT.create(PoolingRepositoryService.class);
    @UiField
    VerticalPanel questionsPanel;

    public QuestionsPanel() {
        initWidget(ourUiBinder.createAndBindUi(this));
    }

    public void setMetaInfoDto(PoolMetaInfoDto metaInfoDto) {
        questionsPanel.clear();
        this.metaInfoDto = metaInfoDto;
        if (metaInfoDto != null) {
            poolingRepository.getQuestions(metaInfoDto.getId(), (LoggCallback<List<PoolQuestionDto>>) poolQuestionDtos -> {
                for (PoolQuestionDto questionDto : poolQuestionDtos) {
                    QuestionCell questionCell = new QuestionCell();
                    questionCell.setQuestionDto(questionDto);
                    questionsPanel.add(questionCell);
                }
            });
        }
    }

    @UiHandler("addQuestionBtn")
    public void addQuestionBtnClick(ClickEvent event) {
        QuestionCell questionCell = new QuestionCell();
        questionsPanel.add(questionCell);
    }
}
